;
var hepler = require("../../javascript/helper");
var request = require("request")
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this;
    //生成产品数据
    this.genProductInfo = function () {
        var projects = [];
        plan = {
            "year": ["16-65"],
            "days": ["1-2d", "3-6d", "7-9d", "10-15d", "16-30d", "1y"],
            "people_count": ["1"],
            "accident_death": ["50000"],
            "accident_medical": ["5000"],
        }
        data = hepler.product([plan["year"], plan["days"], plan["people_count"], plan["accident_death"], plan["accident_medical"]])
        for (i = 0; i < data.length; i++) {
            dt = hepler.formatProductInfo(data[i], plan)
            projects.push(dt);  
        }
        return projects;
    }
    //发送请求
    this.httpRequest = function (dt) {

        if (dt.days != "1y") {
            day = dt.days.replace("d", "天")
        } else {
            day = "一年";
        }

        params = '{"planBaseInfo":{"productCode":"0615kk","planCode":"","cityCode":"10000000","channelCode":"","requestType":"","calculateParameterList":[{"parameterCode":"visitCountryNum","value":"' + day + '"}],"kindList":[],"count":"1"},"head":{"uuid":"1234","tonken":"test","channelNo":"test","checkStr":"test"}}'
        return new Promise(function (resolve, reject) {
            request({
                url: "http://weix.generali-china.cn/mapis/zuul-pageInit/pageInit/plan",
                method: "POST",
                json: false,
                rejectUnauthorized: false,
                headers: {
                    "Host": "weix.generali-china.cn",
                    "Accept": "application/json, text/plain, */*",
                    "Accept-Language": "zh-cn",
                    "Content-Type": "application/json;charset=utf-8",
                    "Origin": "http://weix.generali-china.cn",
                    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.12(0x17000c30) NetType/WIFI Language/zh_CN",
                    "Connection": "keep-alive",
                    "Cookie": "jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VFk_iFpS8kYgck6f3NX7lN9DgwXjXfIttqsXBNCpk3SAMuZwQgk7a7mEKhoCcPCWkg"

                },
                body: params
            }, function (err, res, body) {
                if (!err && res.statusCode == 200) {
                    if (body.indexOf("成功") != -1 && JSON.parse(body).planList.length != 0) {
                        list = JSON.parse(body).planList;

                        resolve(list);
                    }
                }
            })

        })

    }
    //执行爬虫
    this.main = async function () {
        data = that.genProductInfo();
        for (j = 0; j < data.length; j++) {
            dt = data[j]
            let list = await that.httpRequest(dt);
            for (i = 0; i < list.length; i++) {
                if (list[i].planName == "大球版") {
                    price = list[i].premium.toFixed(2).toString();
                    rs = {
                        "name": "个人运动意外险",
                        "company": "中意财险",
                        "sports": ["足球", "篮球", "排球", "棒球", "垒球"],
                        "price": price
                    }
                    

                } else if (list[i].planName == "小球版") {
                    price = list[i].premium.toFixed(2).toString();
                    rs = {
                        "name": "个人运动意外险",
                        "company": "中意财险",
                        "sports": ["羽毛球", "网球", "乒乓球", "保龄球", "高尔夫球", "壁球"],
                        "price": price
                    }
                    


                } else {
                    price = list[i].premium.toFixed(2).toString();
                    rs = {
                        "name": "个人运动意外险",
                        "company": "中意财险",
                        "sports": ["登山", "野营", "野炊", "真人CS"],
                        "price": price
                    }
                    

                }
                result = Object.assign(dt, rs);
                sports = "'" + JSON.stringify( result.sports ) + "'";
                
                let Sql = 'INSERT INTO `accident`(`year`,`days`,`people_count`,`accident_death`,`accident_medical`,`name`,`company`,`sports`,`price`)VALUES("'+result.year+'","'+result.days+'","'+result.people_count+'","'+result.accident_death+'","'+result.accident_medical+'","'+result.name+'","'+result.company+'",'+sports+',"'+result.price+'")'
                mysql.db.query( Sql,function( err,data ){
                    if(!err){
                        console.log( Sql );
                    }else{
                        console.log("数据写入数据库错误")
                    }
                })
            }

        }
        mysql.close();

    }


}
